from django.db.models import Q
from django.shortcuts import render,redirect,HttpResponse
from textapp import models
from django.http import JsonResponse

# Create your views here.


def decorate(f):
	def fin(request,*args):
		if request.COOKIES.get('login_status') == 'T':
			return f(request,*args)
		else:
			return redirect('login')
	return fin


def add(request):
	if request.method == "GET":
		return render(request,"add.html")
	else:
		models.Usertable.objects.create(
			username=request.POST.get("username"),
			password=request.POST.get("password"),
			tel=request.POST.get("tel"),
			sex=request.POST.get("sex"),
			addr=request.POST.get("addr"),
			caracter=request.POST.get("caracter"),
		)
		return redirect("/home/")


def mainpage(request):
	return render(request,"mainpage.html")


def login(request):
	if request.method == 'GET':
		return render(request, 'login.html')
	else:
		username = request.POST.get('username')
		password = request.POST.get('password')
		if username == "red" and password == "123":
			ret = redirect('/home/')
			ret.set_cookie('login_status', 'T')
			return ret
		else:
			return HttpResponse("账号或密码错误")


@decorate
def home(reqeust):
	lst = [i for i in models.Usertable.objects.filter()]
	return render(reqeust,"home.html",{"ls":lst})


@decorate
def edit(request,uid):
	if request.method == "GET":
		deft = models.Usertable.objects.get(id=uid)
		return render(request,"templates/edit.html",{"deft":deft})
	else:
		models.Usertable.objects.filter(id=uid).update(
			username=request.POST.get("username"),
			password=request.POST.get("password"),
			tel=request.POST.get("tel"),
			sex=request.POST.get("sex"),
			addr=request.POST.get("addr"),
			caracter=request.POST.get("caracter"),
		)
		return redirect("/home/")


@decorate
def find(request):
	find1 = request.POST.get("fname")
	find2 = request.POST.get("ftel")
	find3 = request.POST.get("fsex")
	# 啥也没输入
	if find1 == "" and find2 == "" and find3 == "":
		findlist = [i for i in models.Usertable.objects.filter()]
		print(findlist)
		return render(request, "find.html", {"findlist": findlist})
	# 仅输入了用户名
	elif find1 != "" and find2 == "" and find3 == "":
		findlist = [i for i in models.Usertable.objects.filter(username__contains=find1)]
		return render(request, "find.html", {"findlist": findlist})
	# 仅输入了手机号
	elif find1 == "" and find2 != "" and find3 == "":
		findlist = [i for i in models.Usertable.objects.filter(tel__contains=find2)]
		return render(request, "find.html", {"findlist": findlist})
	# 仅输入了性别
	elif find1 == "" and find2 == "" and find3 != "":
		findlist = [i for i in models.Usertable.objects.filter(sex=find3)]
		return render(request, "find.html", {"findlist": findlist})
	# 输入了性名和手机号
	elif find1 != "" and find2 != "" and find3 == "":
		findlist = [i for i in models.Usertable.objects.filter(Q(username__contains=find1) and Q(tel__contains=find2))]
		return render(request, "find.html", {"findlist": findlist})
	# 输入了手机号和性别????????????????
	elif find1 == "" and find2 != "" and find3 != "":
		findlist = [i for i in models.Usertable.objects.filter(Q(sex=find3) and Q(tel=find2))]
		print(findlist)
		return render(request, "find.html", {"findlist": findlist})
	# 输入了姓名和性别
	elif find1 != "" and find2 == "" and find3 != "":
		findlist = [i.id for i in models.Usertable.objects.filter(Q(username__contains=find1) and Q(sex=find3))]
		print(findlist)
		return render(request, "find.html", {"findlist": findlist})


@decorate
def muser(request):
	return render(request,"muser.html")


@decorate
def mcaracter(request):
	return render(request,"mcaracter.html")


def delete(request,uid):
	print(uid)
	models.Usertable.objects.filter(id=uid).delete()
	return redirect("/home/")


def rigister(request):
	if request.method == "GET":
		return render(request,"templates/rigister.html")
	else:
		data = request.POST
		ls = [i.name for i in models.User.objects.filter()]
		username = data.get('username')
		passwordi = data.get('passwordi')
		passwordc = data.get('passwordc')
		print(ls)
		if username not in ls and passwordi == passwordc:
			models.User.objects.create(
				name=username,
				password=passwordc,
			)
			print(ls)
			return HttpResponse("success")
		else:
			ret = HttpResponse("error")
			ret.status_code = 400
			return ret


def ajax_del_user(request, user_id):
	try:
		models.Usertable.objects.get(id=user_id).delete()
		res_data = {'status': 1, 'msg': '删除成功!'}
	except:
		res_data = {'status': 0, 'msg': '删除失败!'}
	return JsonResponse(res_data)

